export class TreeWalker {
  root_ = null;
  current_ = null;
  close_ = null;
  constructor(mnode) {
    this.root_ = mnode;
    this.current_ = mnode;
    this.close_ = false;
  }
  next() {
    let current = this.current_;
    let close = this.close_;
    if (current === null) {
      return;
    }
    if (!close && current.isContainer) {
      if (current.firstChild) {
        this.current_ = current.firstChild;
        this.close_ = false;
      } else {
        this.close_ = true;
      }
    } else if (current === this.root_) {
      this.current_ = null;
    } else if (current.next == null) {
      this.current_ = current.parent;
      this.close_ = true;
    } else {
      this.current_ = current.next;
      this.close_ = false;
    }
    return { mnode: current, close };
  }
}

export default TreeWalker;
